<template>
  <div>
    <el-card class="box-card">
      <!-- 面包屑 -->
      <div class="arrowRight">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>商品管理</el-breadcrumb-item>
          <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 添加分类 -->
      <p class="btn">
        <el-button type="primary" @click="addfl">添加分类</el-button>
      </p>
      <!-- 商品列表 -->
      <div class="list">
        <el-table
          :data="tableData"
          style="width: 100%"
          row-key="cat_id"
          border
          lazy
          height="430px"
          size="mini"
          :tree-props="{ children: 'children' }"
        >
          <el-table-column
            type=""
            prop="idx"
            align="center"
            label="序号"
            width="50"
          >
          </el-table-column>
          <el-table-column prop="cat_name" align="center" label="分类名称">
          </el-table-column>
          <el-table-column prop="cat_deleted" align="center" label="是否有效">
            <template slot-scope="level">
              <i
                class="el-icon-circle-check"
                style="color: bule"
                v-if="level.row.cat_deleted == false"
              ></i>
              <i class="el-icon-circle-close" v-else></i>
            </template>
          </el-table-column>
          <el-table-column prop="cat_level" align="center" label="层级">
            <template slot-scope="level">
              <!-- {{ level.row.cat_level }} -->
              <el-tag v-if="level.row.cat_level == 0">一级</el-tag>
              <el-tag v-else-if="level.row.cat_level == 1" type="success"
                >二级</el-tag
              >
              <el-tag v-else type="info">三级</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="handle">
              <!-- {{ (handle.row.cat_id, handle.row.cat_name) }} -->
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="handleEdit(handle.row.cat_id, handle.row.cat_name)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="delfl(handle.row.cat_id, handle.row.cat_name)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tobal"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 编辑对话框 -->
    <el-dialog title="修改分类" :visible.sync="editDialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleForm.cat_name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="editConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加分类 -->
    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="40%">
      <el-form
        :model="addRuleForm"
        :rules="rules"
        ref="addRuleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addRuleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            style="width: 100%"
            v-model="a"
            :options="options"
            clearable
            :props="{
              checkStrictly: true,
              label: 'cat_name',
              value: 'cat_id',
            }"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      //获取商品列表参数
      params: {
        type: [3],
        pagenum: 1,
        pagesize: 10,
      },
      tobal: 0,
      currentPage4: 0,
      //添加分类对话框
      addDialogVisible: false,
      addRuleForm: {
        cat_name: "",
      },
      //编辑分类对话框
      editDialogVisible: false,
      //编辑from表单
      ruleForm: {
        cat_name: "",
      },
      id: "",
      rules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 3, max: 9, message: "长度在 3 到 9 个字符", trigger: "blur" },
        ],
      },
      //添加分类
      options: [],
      a: [],
    };
  },
  methods: {
    //删除分类
    async delfl(id, name) {
      let res = await this.axios.delete(`categories/${id}`);
      if (res.data.meta.status == 200) {
        this.$message({
          type: "success",
          message: `${name}分类删除成功！`,
          center: "true",
        });
        this.goodsList();
      } else {
        this.$message({
          type: "success",
          message: `${name}分类删除失败！`,
        });
      }
    },
    //确认添加分类
    async addForm() {
      let res = await this.axios.post("categories", {
        cat_pid: this.a[this.a.length - 1],
        cat_name: this.addRuleForm.cat_name,
        cat_level: this.a.length,
      });
      console.log(res);
      if (res.data.meta.status == 201) {
        this.$message({
          type: "success",
          message: "添加分类成功！",
          center: "true",
        });
        this.addDialogVisible = false;
        this.goodsList();
      } else {
        this.$message({
          type: "success",
          message: "添加分类失败！",
          center: "true",
        });
      }
    },
    async addfl() {
      this.addDialogVisible = true;
      let res = await this.axios.get("categories", {
        params: {
          type: [2],
        },
      });
      console.log(res);
      this.options = res.data.data;
    },
    addConfirm() {},
    //分类编辑确定
    editConfirm() {
      this.$refs["ruleForm"].validate((val) => {
        if (val) {
          this.axios.put(`categories/${this.id}`, this.ruleForm).then(
            (res) => {
              if (res.data.meta.status == 200) {
                this.$message({
                  type: "success",
                  message: "修改成功！",
                  center: "true",
                });
                this.editDialogVisible = false;
                this.goodsList();
              } else {
                this.$message({
                  type: "success",
                  message: "修改失败！",
                  center: "true",
                });
              }
            },
            (err) => {
              console.log(err);
            }
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //编辑商品
    handleEdit(id, name) {
      this.editDialogVisible = true;
      this.id = id;
    },
    //每页多少条数据
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.pagesize = val;
      this.goodsList();
    },
    //当前页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.pagenum = val;
      this.goodsList();
    },
    //获取商品列表
    async goodsList() {
      let res = await this.axios.get("categories", {
        params: this.params,
      });
      console.log(res.data.data.result);
      this.tableData = res.data.data.result;
      this.tobal = res.data.data.total;
      res.data.data.result.forEach((element, index) => {
        element.idx = index + 1;
      });
    },
  },
  mounted() {
    this.goodsList();
  },
};
</script>

<style lang="less" scoped>
div {
  .box-card {
    width: 100%;
    .el-cascader {
      width: 340px !important;
    }
    .btn {
      margin: 25px 5px;
    }
    .list {
      margin: 10px 5px;
    }
    .block {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>